package com.exp.base.service;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import com.exp.base.annotation.TransactionalRead;
import com.exp.base.annotation.TransactionalWrite;
import com.exp.base.dao.HibernateDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
@Deprecated
public class BaseServiceHibernate<T> {
	/** 实体类类型 */
	private Class<T> entityClass;

	@Resource
	HibernateDao hibernateDao;
	
	public BaseServiceHibernate() {
		Type type = getClass().getGenericSuperclass();
		Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
		entityClass = (Class<T>) parameterizedType[0];
	}
	
	@TransactionalWrite
	public void save(T t){
		hibernateDao.save(t);
	}
	
	@TransactionalWrite
	public void update(T t){
		hibernateDao.update(t);
	}
	
	@TransactionalWrite
	public void saveOrUpdate(T t){
		hibernateDao.saveOrUpdate(t);
	}
	
	@TransactionalWrite
	public void delete(T t){
		hibernateDao.delete(t);
	}
	
	@TransactionalWrite
	public void delete(Serializable... ids) {
		if (ids != null) {
			for (Serializable id : ids) {
				delete(hibernateDao.get(entityClass,id));
			}
		}
	}
	
	@TransactionalWrite
	public void softDelete(T t){
		hibernateDao.softDelete(t);
	}
	
	@TransactionalWrite
	public void softDelete(Serializable... ids) {
		if (ids != null) {
			for (Serializable id : ids) {
				softDelete(hibernateDao.get(entityClass,id));
			}
		}
	}
	
	@TransactionalRead
	public T get(Serializable id) {
		return (T) hibernateDao.get(entityClass, id);
	}
	
	@TransactionalRead
	public long size(Map<String,Object> params){
		return hibernateDao.size(entityClass, params);
	}
	
	@TransactionalRead
	public boolean exist(DetachedCriteria detachedCriteria){
		Criteria criteria = detachedCriteria.getExecutableCriteria(hibernateDao.getCurrentSession());
		return hibernateDao.exist(criteria);
	}
	
	@TransactionalRead
	public boolean exist(Map<String,Object> params){
		DetachedCriteria detachedCriteria = hibernateDao.getDetachedCriteria(entityClass,params);
		return exist(detachedCriteria);
	}
	
	@TransactionalRead
	public T find(DetachedCriteria detachedCriteria) {
		Criteria criteria = detachedCriteria.getExecutableCriteria(hibernateDao.getCurrentSession());
		return hibernateDao.find(criteria);
	}
	
	@TransactionalRead
	public T find(Map<String,Object> params) {
		DetachedCriteria detachedCriteria = hibernateDao.getDetachedCriteria(entityClass,params);
		return find(detachedCriteria);
	}
	
	@TransactionalRead
	public PageInfo<T> query(Page<T> page,DetachedCriteria detachedCriteria) {
		Criteria criteria = detachedCriteria.getExecutableCriteria(hibernateDao.getCurrentSession());
		return hibernateDao.query(page,criteria);
	}
	
	@TransactionalRead
	public PageInfo<T> query(Page<T> page,Map<String,Object> params) {
		DetachedCriteria detachedCriteria = hibernateDao.getDetachedCriteria(entityClass,params);
		return query(page,detachedCriteria);
	}

}
